<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="../images/yc.png" type="image/x-icon">
    <link rel="stylesheet" href="../css/layui.css" media="all">
</head>
<body>
<br />
<form class="layui-form" id="myform" style="width: 90%"
      onsubmit="return false">
    <div class="layui-form-item">
        <label class="layui-form-label">模板名</label>
        <div class="layui-input-inline" style="width: 25%">
            <input type="text" name="templateName" lay-verify="required"
                   lay-reqtext="模板名不能为空..." placeholder="请输入模板名" autocomplete="off"
                   class="layui-input" style="width: 400px;" id="templateName">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">模板内容</label>
            <div class="layui-input-block">
                <textarea name="goods_form_descr" id="goods_form_descr" rows="10" cols="80"></textarea>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit=""
                    lay-filter="demo1">新增模板</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="../js/layui.all.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/ajaxfileupload.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/showpic.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/axios.js" charset="utf-8"></script>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script>
    CKEDITOR.replace('goods_form_descr');
    layui.use(['form'], function(){
        var form = layui.form,
            layer = layui.layer;
        //获取要修改的信息
        let upId = sessionStorage.getItem("upId")
        let cbt = {}
        $.get("/tender/thisTenderInfo",{id:upId}).then(resp=>{
            let item = resp.data
            cbt =  resp.data
            console.log(item)
            $("#bdNo").val(item.bdNo)
            $("#title").val(item.title)
            $("#goods_form_descr").val(item.content)
        },"json")


        // 自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 2){
                    return '帖子名称至少得2个字符...';
                }
            }
        });

        // 监听提交
        form.on('submit(demo1)', function(data){
            let obj = data.field;
            let descr = CKEDITOR.instances.goods_form_descr.getData();
            console.log(obj)
            obj.template = descr;
            $.ajaxFileUpload({
                url:"/template/add",
                secureuri:false,
                data:obj,
                dataType:"json",
                success:function(rt,status){
                    if(rt.code == 511){
                        layer.msg("输入信息不完整,请确认后提交...");
                        return false;
                    }

                    if(rt.code == 200){
                        $("#myform")[0].reset();   //重置表单
                        $("#showpic").html("");  //清空图片预览
                        CKEDITOR.instances.goods_form_descr.setData("");
                        layui.form.render();
                        layer.msg("添加成功...",{icon:1,time:2000},function (){
                            sessionStorage.removeItem("upId")
                            location.href="tenderAnnouncement.html"
                        });
                        return false;
                    }
                    layer.msg("添加失败...",{icon:2,time:2000});
                },
                error:function(rt,status,e){
                    console.log(data)
                    layer.msg("添加失败\n" + e,{icon:2,time:2000})
                }
            })
            return false;
        });
    });
</script>
</body>
</html>